package com.xqboss.web.controller.guild;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xqboss.apps.domain.guild.Guild;
import com.xqboss.apps.dto.PageDto;
import com.xqboss.apps.dto.guild.GetGuildListDto;
import com.xqboss.apps.dto.guild.GuildSaleableDto;
import com.xqboss.apps.dto.guild.WebGuildEquityConfigDto;
import com.xqboss.apps.service.user.UserIdentityPermGuildService;
import com.xqboss.apps.service.guild.GuildService;
import com.xqboss.apps.vo.guild.GuildDetailVo;
import com.xqboss.apps.vo.guild.WebGuildEquityConfigVo;
import com.xqboss.apps.vo.guild.WebGuildVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.PageResult;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @author Administrator
 */
@Api(tags = {"公会"})
@RestController
@RequestMapping("/web/guild")
@Slf4j
public class WebGuildController extends BaseController {

    @Autowired
    private GuildService guildService;

    @Autowired
    private UserIdentityPermGuildService userIdentityPermGuildService;

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:guild:add')")
    @ApiOperation("新增公会")
    @PostMapping("add")
    @Log(title = "公会", businessType = BusinessType.INSERT)
//    public R<String> add(@Validated @RequestBody WebGuildDto dto) {
    public R<String> add(@RequestParam(name = "userList")
                         @ApiParam(name = "userList",value = "会长用户id数组",required = true)
                         @Size(min = 1, message = "至少公会给一个用户") List<String> userList) {
        try {
            guildService.add(userList);
            return R.ok_msg("新增公会成功！");
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('web:guild:list')")
    @ApiOperation("公会列表")
    @GetMapping("list")
    public PageResult<WebGuildVo> list(@Validated GetGuildListDto<WebGuildVo> dto) {
        IPage<WebGuildVo> page = guildService.getGuildList(dto);
        return PageResult.ok(page,"公会列表获取成功~");
    }

    @PreAuthorize("@ss.hasPermi('web:guild:detail')")
    @ApiOperation("公会详情")
    @GetMapping("detail/{id}")
    public R<GuildDetailVo> detail(@PathVariable("id")
                                   @ApiParam(value = "公会ID", required = true)
                                   @NotNull(message = "公会ID不能为空")
                                   Long id,@Validated PageDto dto) {
        dto.startPage();
        GuildDetailVo vo = guildService.getGuildDitle(id);
        return R.ok(vo);
    }

    @PreAuthorize("@ss.hasPermi('web:guild:update')")
    @ApiOperation("编辑公会")
    @PutMapping("update")
    @Log(title = "公会", businessType = BusinessType.UPDATE)
    public R update(@Validated @RequestBody Guild dto) {
        guildService.editGuild(dto);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:guild:update-saleable')")
    @ApiOperation("公会上下架")
    @PutMapping("update-saleable")
    @Log(title = "公会", businessType = BusinessType.UPDATE)
    public R updateSaleable(@Validated @RequestBody GuildSaleableDto dto) {
        guildService.changeSaleable(dto);
        return R.ok();
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:guild:dissolve')")
    @ApiOperation("解散公会")
    @PutMapping("dissolve/{guildId}")
    @Log(title = "解散公会", businessType = BusinessType.UPDATE)
    public R guildDissolve(@PathVariable(name = "guildId") @ApiParam(name = "guildId",value = "公会id",required = true) @NotNull(message = "公会id不能为空") Long guildId) {
        guildService.guildDissolve(guildId);
        return R.ok();
    }


    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:guild:equityConf-saveOrUpdate')")
    @ApiOperation("公会权益配置-新增或修改")
    @PostMapping("equityConf-saveOrUpdate")
    @Log(title = "公会权益配置-新增或修改", businessType = BusinessType.INSERT_OR_UPDATE)
    public R<String> saveOrUpdateEquityConfig(@Validated @RequestBody WebGuildEquityConfigDto dto){
        String msg = dto.getId()==null?"新增":"修改";
        try {
            userIdentityPermGuildService.saveOrUpdateEquityConfig(dto);
            log.info("公会权益配置成功[{}]！",msg);
            return R.ok_msg("公会权益配置成功!");
        }catch (Exception e){
            log.error("[equityConfigInfo err]-{}",msg,e);
            return R.fail(e.getMessage());
        }
    }

    @PreAuthorize("@ss.hasPermi('web:guild:equityConf-get')")
    @ApiOperation("公会权益配置-获取")
    @GetMapping("equityConf-get/{identityId}")
    public R<WebGuildEquityConfigVo> getGuildEquityConfig(@PathVariable("identityId")
                                                          @ApiParam(value = "身份id", required = true)
                                                          @NotNull(message = "身份id不能为空！") Long identityId){
        try {
            WebGuildEquityConfigVo config = userIdentityPermGuildService.getGuildEquityConfig(identityId);
            log.info("获取公会权益配置成功：{}",config);
            return R.ok(config,"获取公会权益配置成功!");
        }catch (Exception e){
            log.error("[getGuildEquityConfig err]",e);
            return R.fail(e.getMessage());
        }
    }
}
